﻿using DCAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinalTestMachine.Motion.Aixs;
using FinalTestMachine.Motion.IO;
using FinalTestMachine.Motion.Process;
using FinalTestMachine.Motion.Product;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// Unit_MainBtns_2.xaml 的交互逻辑
    /// </summary>
    public partial class Unit_MainBtns_2 : UserControl
    {
        public Unit_MainBtns_2()
        {
            InitializeComponent();
            IUserLimitdll.PreControlBindingHelper.SetLitmitBinding(btn_SwitchStation, IUserLimitdll.UserLevelModes.ModifyAndOperation);

            //光管角度确认
            //IUserLimitdll.PreControlBindingHelper.SetLitmitBinding(btn_LightPipeConfirm, IUserLimitdll.UserLevelModes.ModifyAndOperation);
        }

        private void btn_SwitchStation_Click(object sender, RoutedEventArgs e)
        {
            if(MessageBoxResult.Yes != MessageBox.Show("请确认操作？" , "tips", MessageBoxButton.YesNo))
            {
                return;
            }
            
            try
            {
                btn_SwitchStation.IsEnabled = false;
                this.Dispatcher.Invoke(() =>
                {
                    try
                    {
                        RotatedMovNextHelper.MovNext();
                        btn_SwitchStation.IsEnabled = true;
                    }
                    catch (Exception ex)
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            MessageBox.Show(ex.Message);
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Btn_ResMachine_Click(object sender, RoutedEventArgs e)
        {
            if (!StaticIOHelper.Fun_CheckSocketClosed())
            {
                if (MessageBoxResult.Yes == MessageBox.Show("检测到工位上有治具没有闭合，请问是否要继续机器复位操作？",
                "请确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No))
                {
                    Motion.StaticInitial.Motion.CommitResOrder();
                }
                else
                {
                    return;
                }
            }
            else
            {
                Motion.StaticInitial.Motion.CommitResOrder();
            }

        }

        private void Btn_ClearErr_Click(object sender, RoutedEventArgs e)
        {
            Motion.StaticInitial.Motion.CommitClearErrOrder();
        }

        private void Btn_Start_Click(object sender, RoutedEventArgs e)
        {
            if (this.btn_Start.Content.ToString() != "继续")
            {
                bool exist1 = false;
                bool exist2 = false;
                for(int i = 0; i < GlobalParams.WorkstationSum; i++)
                {
                    if (!exist1 && GlobalParams.GetFieldValue<bool>(Funs.FunSocketSettings.Cur, $"IsUseSocket{(char)(65 + i)}"))
                    {
                        exist1 = true;
                    }

                    if (!exist2 && GlobalParams.GetFieldValue<bool>(Funs.FunStationSettings.Cur, $"Site{i+1}IsUse"))
                    {
                        exist2 = true;
                    }
                }
                if (!exist1 || !exist2) {
                    MessageBox.Show("检测到当前设备的全部载具或测试工位都处于屏蔽状态，无法进入自动运行模式");
                    return;
                }
            }

            Motion.StaticInitial.Motion.CommitStartRunOrder();
        }

        private void Btn_Stop_Click(object sender, RoutedEventArgs e)
        {
           Motion.StaticInitial.Motion.CommitStopOrder();
        }

        RM_dll2.StandardOrderHelper StandardOrderHelper => Motion.StaticInitial.StandardOrder;


        public void ShowUI()
        {
            if (StandardOrderHelper.IsErr || StandardOrderHelper.IsAlarm ||
                StandardOrderHelper.IsEMG || StandardOrderHelper.IsResing ||
                StandardOrderHelper.IsAutoRunning)
            {
                btn_ResMachine.IsEnabled = false;
                btn_SwitchStation.IsEnabled = false;
            }
            else
            {
                btn_ResMachine.IsEnabled = true;

                if (!Aix_Turn.Cur.IsHomeOK || Aix_Turn.Cur.GetAixRunState() || !Aix_Turn.Cur.GetAixSvon())
                {
                    btn_SwitchStation.IsEnabled = false;
                }
                else
                {
                    btn_SwitchStation.IsEnabled = true;
                }
            }


            if (StandardOrderHelper.IsErr || StandardOrderHelper.IsAlarm ||
                StandardOrderHelper.IsEMG || StandardOrderHelper.IsResing ||
                !StandardOrderHelper.IsResOK || StandardOrderHelper.IsAutoRunning && !(StandardOrderHelper.IsErrPause || StandardOrderHelper.IsPause))
            {
                btn_Start.IsEnabled = false;
            }
            else
            {
                btn_Start.IsEnabled = true;
            }

            if (StandardOrderHelper.IsErr)
            {
                btn_ClearErr.IsEnabled = true;
            }
            else
            {
                btn_ClearErr.IsEnabled = false;
            }

            if (StandardOrderHelper.IsAutoRunning)
            {
                chb_ModeSelect.IsEnabled = true;
                if (StandardOrderHelper.IsPause)
                {
                    btn_Start.LanContent = "继续";
                }
                else
                {
                    btn_Start.LanContent = "启动";
                }
            }
            else
            {
                Motion.Process.Process_AutoRun.Cur.IsClearMode = false;
            }

        }

        private void Chb_ModeSelect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Motion.Process.Process_AutoRun.Cur.IsClearMode = chb_ModeSelect.IsChecked == true;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 是否启用快捷双启动按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chn_Dualboot_Click(object sender, RoutedEventArgs e)
        {
            //if (chn_Dualboot.IsChecked==true)
            //{
            //    btn_Dualboot.IsEnabled = true;
            //}
            //else
            //{
            //    btn_Dualboot.IsEnabled = false;
            //}
        }
        /// <summary>
        /// 双启动按钮，1、在按钮开关按下检测不到输入信号是使用，2、快捷使用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Dualboot_Click(object sender, RoutedEventArgs e)
        {
            IOScanHelper.IsLoadStart = true;
        }

        //private void Button_65V_Click(object sender, RoutedEventArgs e)
        //{
        //    Connect.Site3Station_Connect.SetVol(Connect. ConnectFactory.PowerSerialPort_1, Power3Demo.ChannelIndex.First, 6.5);
        //}

        //private void Button_55V_Click(object sender, RoutedEventArgs e)
        //{
        //    Connect.Site3Station_Connect.SetVol(Connect.ConnectFactory.PowerSerialPort_1, Power3Demo.ChannelIndex.First, 5.5);
        //}
    }
}
